package com.apicloud.moduleAppManager;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.annotation.UzJavascriptMethod;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;

/**
 * 该类映射至Javascript中moduleApp对象<br>
 * <br>
 * <strong>Js Example:</strong><br>
 * var module = api.require('moduleApp');<br>
 * module.xxx();
 * 
 */
public class ModuleApp extends UZModule {

	public ModuleApp(UZWebView webView) {
		super(webView);
	}

	// 安装软件信息
	// 安装包信息
	public class AppInfo {
		private String name;
		private String ver;
		private Drawable drw;
		private String PackageName;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getVer() {
			return ver;
		}

		public void setVer(String ver) {
			this.ver = ver;
		}

		public Drawable getDrw() {
			return drw;
		}

		public void setDrw(Drawable drw) {
			this.drw = drw;
		}

		public String getPackageName() {
			return PackageName;
		}

		public void setPackageName(String packageName) {
			PackageName = packageName;
		}
	}

	// 列举安装的包
	public class PakageInfoProvider {
		private Context context;
		private List<AppInfo> appInfos;
		private AppInfo appInfo;

		public PakageInfoProvider(Context context) {
			super();
			this.context = context;
		}

		public List<AppInfo> getAppInfo() {
			PackageManager pm = context.getPackageManager();
			List<PackageInfo> pakageinfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

			appInfos = new ArrayList<AppInfo>();
			for (PackageInfo packageInfo : pakageinfos) {
				appInfo = new AppInfo();

				// 获取应用程序的名称，不是包名，而是清单文件中的labelname
				String str_name = packageInfo.applicationInfo.loadLabel(pm).toString();
				appInfo.setName(str_name);

				// 获取应用程序的版本号码
				String version = packageInfo.versionName;
				appInfo.setVer(version);

				// 获取应用程序的快捷方式图标
				// Drawable drawable = packageInfo.applicationInfo.loadIcon(pm);
				// appInfo.setDrw(drawable);

				// 给一同程序设置包名
				appInfo.setPackageName(packageInfo.packageName);

				appInfos.add(appInfo);
				appInfo = null;
			}

			return appInfos;
		}
	}

	/**
	 * <strong>函数</strong><br>
	 * <br>
	 * 该函数映射至Javascript中moduleUtile对象的listAppInfo函数<br>
	 * <br>
	 * <strong>JS Example：</strong><br>
	 * JSONObject[] = moduleDemo.listApp();
	 * 
	 */
	@UzJavascriptMethod
	public List<JSONObject> jsget_listAppInfo() {
		List<JSONObject> retJsons = new ArrayList<JSONObject>();

		List<AppInfo> appInfos;
		PakageInfoProvider pakageInfoProvider = new PakageInfoProvider(this.getContext());
		appInfos = pakageInfoProvider.getAppInfo();
		for (int i = 0; i < appInfos.size(); i++) {
			String str = appInfos.get(i).getPackageName();
			if (str.contains("android") || str.contains("huawei")) {
				continue;
			}

			JSONObject retJson = new JSONObject();
			try {
				retJson.put("pkg", appInfos.get(i).getPackageName());
				retJson.put("name", appInfos.get(i).getName());
			} catch (JSONException e) {
				e.printStackTrace();
			}

			retJsons.add(retJson);
		}

		return retJsons;
	}

}
